package com.wm.machine.baselibrary.utils;

import android.text.TextUtils;
import android.util.Log;

import java.text.SimpleDateFormat;

/**
 * Author： cxp
 * CreatTime： 2016/11/24
 * ContactInfo：
 * Description: Log tools
 */
public final class LogUtils {
  private static String mTag = "CLOG";
  private static String fileName;
  private static String className;
  private static String methodName;
  private static int lineNumber;
  private static String codeLink;
  private static boolean isDebug = true;
  private static final int THREE = 3;

  private LogUtils() {
    throw new UnsupportedOperationException("LogUtils cannot be instantiated");
  }

  /**
   * 全局控制log日志输出
   *
   * @param isDebug true：输出，false:关闭
   */
  public static void setDebug(boolean isDebug) {
    isDebug = isDebug;
  }

  /**
   * 设置log 的mTag
   *
   * @param mTag logcat 中的Tag
   */
  public static void setLogmTag(String mTag) {
    mTag = mTag;
  }

  /** 
   * 获取线程堆栈信息
   *
   * @param element StackTraceElement 对象
   */
  private static void getParameters(StackTraceElement element) {
    fileName = element.getFileName();
    if (TextUtils.isEmpty(mTag)) {
      mTag = fileName;
    }
    className = element.getClassName();
    methodName = element.getMethodName();
    lineNumber = element.getLineNumber();
    codeLink = new StringBuffer().append("CodeLink      --------  ").append("(").append(fileName)
        .append(":").append(lineNumber).append(")").toString();
  }

  /** 
   * 组合log信息
   *
   * @param log log信息
   * @return 组合后的log
   */
  private static String buildLog(String log) {
    long timeMillis = System.currentTimeMillis();
    String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(timeMillis);
    StringBuffer buffer = new StringBuffer();
    buffer.append("Time          --------  ").append(time);
    buffer.append("\nThreadName    --------  ").append(Thread.currentThread().getName());
    buffer.append("\nFileName      --------  ").append(fileName);
    buffer.append("\nClassName     --------  ").append(className);
    buffer.append("\nMethodName    --------  ").append(methodName);
    buffer.append("\nLineNum       --------  ").append(lineNumber);
    buffer.append("\nMessage       --------  ").append(log);
    return buffer.toString();
  }

  /** 
   *
   * @param message 信息字符串
   */
  public static void d(String message) {
    if (isDebug) {
      getParameters(Thread.currentThread().getStackTrace()[THREE]);
      Log.d(mTag, codeLink);
      Log.d(mTag, buildLog(message));
    }
  }

  /** 
   *
   * @param message 信息字符串
   */
  public static void i(String message) {
    if (isDebug) {
      getParameters(Thread.currentThread().getStackTrace()[THREE]);
      Log.i(mTag, codeLink);
      Log.i(mTag, buildLog(message));
    }
  }

  /** 
   *
   * @param message 信息字符串
   */
  public static void w(String message) {
    if (isDebug) {
      getParameters(Thread.currentThread().getStackTrace()[THREE]);
      Log.w(mTag, codeLink);
      Log.w(mTag, buildLog(message));
    }
  }

  /**
   *
   * @param message 信息字符串
   */
  public static void e(String message) {
    if (isDebug) {
      getParameters(Thread.currentThread().getStackTrace()[THREE]);
      Log.e(mTag, codeLink);
      Log.e(mTag, buildLog(message));
    }
  }
}
